package com.wq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.common.PageResult;
import com.wq.common.Result;
import com.wq.dto.TagAddDTO;
import com.wq.dto.TagDeleteDTO;
import com.wq.dto.TagPageDTO;
import com.wq.entity.Tag;
import com.wq.service.TagService;
import com.wq.vo.TagListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-10-17
 */
@Slf4j
@RestController
@RequestMapping("/admin/tag")
public class TagController {
@Resource
    private TagService tagService;
    @PostMapping("add")
    private Result<Object> add(@RequestBody TagAddDTO tagAddDTO){
        List<Tag> tags = tagAddDTO.getTags();
        tagService.saveBatch(tags);
        return Result.success();
    }

    @PostMapping("delete")
    private Result<Object> delete(@RequestBody TagDeleteDTO tagDeleteDTO){
        tagService.removeById(tagDeleteDTO.getTagId());
        return Result.success();
    }
    @PostMapping("list")
    public PageResult<Tag> list(@RequestBody TagPageDTO tagPageDTO){
        log.info("{}",tagPageDTO);
        IPage<Tag> page = new Page<>(tagPageDTO.getCurrent(),tagPageDTO.getSize());
        LambdaQueryWrapper <Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(tagPageDTO.getTagName()!=null,Tag::getName,tagPageDTO.getTagName());
        wrapper.between(tagPageDTO.getStartDate()!=null,Tag::getCreateTime,tagPageDTO.getStartDate(),tagPageDTO.getEndDate());
        IPage<Tag> page1 = tagService.page(page, wrapper);
        return PageResult.success(page1);
    }
    @PostMapping("select/list")
    public Result<List<TagListVO>> selectList(){
        List<TagListVO> list =tagService.selectList();
        return Result.success(list);

    }
}
